package cn.edu.neusoft.mall.service.impl;

import cn.edu.neusoft.mall.Dao.UserDao;
import cn.edu.neusoft.mall.Dao.impl.UserDaoImpl;
import cn.edu.neusoft.mall.entity.User;
import cn.edu.neusoft.mall.service.userService;
import cn.edu.neusoft.framework.Exption.*;

import java.util.Date;
import java.util.UUID;

public class userServiceimpl implements userService {
    UserDao userDao = new UserDaoImpl();

    @Override
    public User getUserByNum(User user) {

        String MD5password = MD5Util.md5(user.getPassword());
        user.setPasswordSecret(MD5password);
        userDao.insertUser(user);
        User userif = userDao.getUserByNum(user.getUsername());

        if (userif == null) {
            if (user.getPassword().equals(user.getPasswordSecret())) {
                if (userDao.insertUser(user) == 1) {
                    return user;
                } else {
                    throw new ServiceException("插入用户数据失败");
                }
            }
        }
        return user;
    }

    @Override
    public User insertUser(User user) {

        User userEntity = userDao.getUserByNum(user.getUsername());
        if (userEntity == null) {
            throw new ServiceException("用户不存在");
        } else {
            String md5Password = MD5Util.md5(user.getPassword());
            if (!md5Password.equals(userEntity.getPasswordSecret())) {
                throw new ServiceException("密码错误");
            } else {
                return userEntity;
            }
        }
    }

    @Override
    public int updateuserde(User user,User user2) {
       return userDao.updateUser(user,user2);
    }
}
